
#include "udp.h"
#include "uv_common.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

////////////////////////test////////////////////////

//#ifndef NDEBUG

#if 1


static void send_end(SendReq_t *req, int status)
{
    printf("%s:%d\n", __func__, status);
    free(req->data);
    free(req);
}

static void udp_recved(udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags)
{
    if (addr->sa_family == PF_INET) {
        char ttt[32];
        uv_ip4_name((const struct sockaddr_in*)addr, ttt, sizeof(ttt));

        printf("from %s:%hu\n", ttt, ntohs(((struct sockaddr_in *)addr)->sin_port));
#if 0
        SendReq_t *req = (SendReq_t *)malloc(sizeof(SendReq_t));
        char *sendbuf = (char *)malloc(buf->len);
        memcpy(sendbuf, buf->base, buf->len);
        udp_send(req, handle, sendbuf, buf->len, addr, NULL);
#else
        SendReq_t *req = (SendReq_t *)malloc(sizeof(SendReq_t));
        req->data = buf->base;
        udp_send(req, handle, buf->base, nread, addr, send_end);
#endif

        //udp_shutdown(NULL, handle, NULL);
        //udp_close(handle);
    } else if (addr->sa_family == AF_INET6) {
        
    } else {

    }

    printf("%s:%zd\n", __func__, nread);
}

void readfromfp(char **buf, size_t *len, FILE *fp)
{
    char tmpdata[4096];
    size_t tmplen = 0;
    int ret = 0;
    *buf = NULL;
    do{
        ret = fread(tmpdata, 1, sizeof(tmpdata), fp);
        if(ret > 0){
            *buf = realloc(*buf, tmplen + ret);
            memcpy((*buf) + tmplen, tmpdata, ret);
            tmplen += ret;
        }
    }while(ret == sizeof(tmpdata));

    int err = ferror(stdin);
    printf("ferror=%d\n", err);
    *len = tmplen;
}




int tstudpmain(int ac, char**av)
{
    //init();
    udp_init();

#define TSTSEND 0
#if TSTSEND
    //test send
    udp_t *newudp = (udp_t *)malloc(sizeof(udp_t));
    udp_createhandler(newudp, NULL, 0);
    
    SendReq_t *req = (SendReq_t *)malloc(sizeof(SendReq_t));

    struct sockaddr_in addr;
    uv_ip4_addr("192.169.133.22", 1234, &addr);

    //char *content;
    //size_t len;
    //readfromfp(&content, &len, stdin);
    //printf("readtotal:%d\n", len);



    char tmpdata[8192];
    int ret = 0;
    
    while(1){
        ret = fread(tmpdata, 1, sizeof(tmpdata), stdin);
        if(ret > 0){
            udp_send(req, newudp, tmpdata, ret, (struct sockaddr*)&addr, NULL);
            usleep(1000);
        }
    }

#else
    udp_t udp;
    struct sockaddr_in addr;
    uv_ip4_addr("0.0.0.0", 1234, &addr);
    udp_createhandler(&udp, (struct sockaddr *)&addr, sizeof(addr));
    udp_recv_start(&udp, udp_recved);
    sleep(10);
    udp_recv_stop(&udp);
    sleep(10);
    udp_recv_start(&udp, udp_recved);

#if 0

    uv_ip4_addr("0.0.0.0", 1235, &addr);
    udp_t udp2;
    udp_createhandler(&udp2, (struct sockaddr *)&addr, sizeof(addr));
    udp_recv_start(&udp2, udp_recved);

#endif
#endif

    while (1) {
        sleep(100);
    }

    return 0;
}

#endif
